/*
    Copyright 2006 Andrew Wilkinson <andrew@indiegigs.co.uk>

    This file is part of GL2x.

    GL2x is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    GL2x is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with GL2x; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#ifndef __GL2X_GL_INTERNAL_H__
#define __GL2X_GL_INTERNAL_H__

#define FIXED 32

#ifdef GP2x_940
#include "gp2x_940t.h"
#else
#include "gp2x.h"
#endif
#include "fixed.h"
#include "gl.h"
#include "maths/maths.h"

enum message {
    DRAW_POLYGON,
    CLEAR_SCREEN,
    CLEAR_DEPTH,
    SET_VIEWPORT,
    SET_CULL_FACE,
    SET_ALPHA_FUNC,
    SET_POLYGON_MODE,
    SET_SHADE_MODEL,
    SET_BLEND_FUNC,
    SET_DEPTH_RANGE,
    SET_DEPTH_MASK,
    SET_DEPTH_FUNC,
    SET_DRAW_BUFFER,
    SET_READ_BUFFER,
    SET_POINT_SIZE,
    SET_LINE_WIDTH,
    SET_TEXTURE,
    BIND_TEXTURE_1D,
    BIND_TEXTURE_2D,
    BIND_TEXTURE_3D,
    GL_ENABLE,
    GL_DISABLE,
    GEN_LISTS,
    NEW_LIST
};

struct glpoly {
    int mode;
    int count;
    int allocsize;
    char allow_delete;
    Vector* vertices;
    Vector* colours;
    Vector* normals;
    Fixed* texcoords;
    Matrix proj;
    Matrix modelview;
};

struct gltextureparams {
    Fixed min_filter;
    Fixed mag_filter;
    Fixed min_lod;
    Fixed max_lod;
    Fixed base_level;
    Fixed max_level;
    Fixed wrap_s;
    Fixed wrap_t;
    Fixed wrap_r;
    Fixed priority;
};

struct glpoly* newpoly();

struct state {
    int error;
    char begin;
    struct glpoly* glpoly;
    Vector colour;
    Vector normal;
    Vector clearcolour;
    Fixed texcoord[2];
    int stack_type;
    matrixstack* mvs;
    matrixstack* pjs;

    GLenum cull_face;

    struct gltextureparams tex1d;
    struct gltextureparams tex2d;
    struct gltextureparams tex3d;

    GLenum texture_env_mode;
    Vector texture_env_color;

    // glViewport values
    int viewport_x;
    int viewport_y;
    int viewport_half_height;
    int viewport_half_width;
};

struct gltexture {
    int dim;
    int width;
    int height;
    void* pixels;
};

struct state* newstate();
void deletestate(struct state* s);

void addvertex(struct glpoly* p, Vector* v);

#ifdef GLCLIENT
extern struct state* current_state;
#endif

#endif
